package stubs;

import java.util.Iterator;
import java.util.LinkedList;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import objetos.ItemVendaObj;
import objetos.VendaObj;
import interfaces.Venda;

public class VendaStub implements Venda {

	@Override
	public float calcularFrete(String cep) {
		try {
			
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Venda/calcularFrete");
			
			// Cria arquivo xml
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("atributo", "atb");
			OMElement payload = factory.createOMElement("cep", ns_element);
			payload.setText(cep);
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			return Float.parseFloat(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public boolean cancelarVenda(int codigoVenda) {
try {
			
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Venda/cancelarVenda");
			
			// Cria elemento
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("atributos", "atb");
			OMElement payload = factory.createOMElement("codigo", ns_element);
			
			payload.setText(new Integer(codigoVenda).toString());
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public void definirFormaPagamento(float valorVenda) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean efetuarVenda(VendaObj Venda,
			LinkedList<ItemVendaObj> produtos) {
		
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Venda/efetuarVenda");
			
			// Cria elemento
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("parametros", "par");
			OMElement payload = factory.createOMElement("parametros", ns_element);
			OMElement lista = factory.createOMElement("lista" , ns_element);
			
			payload.addChild(Venda.toOMElement());
			Iterator<ItemVendaObj> it = produtos.iterator();
			
			while (it.hasNext()) {
				lista.addChild(it.next().toOMElement());
			}
			
			payload.addChild(lista);
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public LinkedList<VendaObj> historicoVendasCliente(int codigoCliente) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Venda/historicoVendasCliente");
			
			// Cria arquivo xml
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("atributo", "atb");
			OMElement payload = factory.createOMElement("codigo", ns_element);
			
			payload.setText(new Integer(codigoCliente).toString());
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			Iterator<OMElement> it = res.getChildElements();
			LinkedList<VendaObj> list = new LinkedList<VendaObj>();
			
			while (it.hasNext()) {
				VendaObj venda = new VendaObj();
				venda.loadOMElement(it.next());
				list.add(venda);
			}
			
			return list;
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
